import requests
from bs4 import BeautifulSoup
import time

# 第一层url = https://cs.fang.anjuke.com/loupan/all/

# 静态爬取每一页中的"https://cs.anjuke.com/community/view/255088"


class GetNewxiangqingList:

    def __init__(self,url):
        self.url = url
        self.cookies = {
            'isp': 'true',
            'isp': 'true',
            'SECKEY_ABVK': 'yN6sMd5ARVvSUB7X+vkXdcFTS+S/5GNyx+spldnhtVI%3D',
            'BMAP_SECKEY': 'MXqX4OT-tHaH65jB1haMsW3c_czRLMOlcXn5KmiudYnIzGogUTkcKkH8_F8GEKgRW2Ry0ol3gIyrlNowW_HolXtoYSSEBo-F0Zn_Rl9YjjTNgxcdXEQmOEk30BE6KCx67MdzQ2nuGqkQAdWO3SsK8kbSWJEFGrvcrfHPpFDZKA9qSZYsUw34WhBJ3eXP7vpy',
            'aQQ_ajkguid': '039DC33C-0142-4899-928D-3306CE716379',
            'sessid': '6A586865-53B3-4025-A44E-41229C751A59',
            'ajk-appVersion': '',
            'ctid': '27',
            'fzq_h': '0a9b12f276e9dcf85279f986229a0221_1704781929199_dd9674afcb7f4b2f9719927a8e40f73d_3740786969',
            'obtain_by': '2',
            'twe': '2',
            'id58': 'CrIfoWWc6GlVO45LJSt2Ag==',
            'ajk_member_verify': 'W5dL2pBhDCiir8akZBDo66rAU%2Bv0ns7yeYLQQhwQQ0U%3D',
            'ajk_member_verify2': 'MjgwNjQwODk2fFVKYm5TcmJ8MQ%3D%3D',
            'ajk_member_id': '280640896',
            '58tj_uuid': '8e523e03-5c23-4504-acff-0874ce7b85d8',
            'init_refer': '',
            'new_uv': '1',
            'als': '0',
            'isp': 'true',
            'new_session': '0',
            'wmda_uuid': 'c4c9f9d9224d8fee21bf554110d012a8',
            'wmda_new_uuid': '1',
            'wmda_session_id_8788302075828': '1704788119056-7b632064-addf-5ff8',
            'wmda_visited_projects': '%3B8788302075828',
            'ved_loupans': '523197',
            'xxzlcid': 'e072e6594d5c42248ac7592958981727',
            'xxzl-cid': 'e072e6594d5c42248ac7592958981727',
            'lp_lt_ut': 'ab3c6d91895043f10b4c919ccc870946',
            'xxzl_cid': 'd71dd6dc33c84bd19340f93a64c3470b',
            'xxzl_deviceid': 'bnNnzFn1rNJNAyVjS3IS6Bc44muBh++I8YMgxvFR33vRRkETlizHoKTKLFAzcNgB',
            'ajkAuthTicket': 'TT=7d4044ba9ee16217635773e28205dc41&TS=1704789514023&PBODY=XRU3EdSHMdJXJwGv0-tMnOU5wNMFLyqehFwf2PA7anPU-luRiVEG5Zueak6kIx5gwzisNUFpx78aYV8-a7p0Mo0jO4VGEXax7wg1UWnQg5LFcb6vReIYDcHpZp-H4qGQ6aqIz2Kqh66Xy5-Rl4tTPCn8Fu6lB9vhYyJt-4eBw8g&VER=2&CUID=z0xo8z93-p5iDJq5J3SnqaS2r3T3qS61',
        }
        self.headers = {
            'authority': 'cs.fang.anjuke.com',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'cache-control': 'max-age=0',
            # 'cookie': 'isp=true; isp=true; SECKEY_ABVK=yN6sMd5ARVvSUB7X+vkXdcFTS+S/5GNyx+spldnhtVI%3D; BMAP_SECKEY=MXqX4OT-tHaH65jB1haMsW3c_czRLMOlcXn5KmiudYnIzGogUTkcKkH8_F8GEKgRW2Ry0ol3gIyrlNowW_HolXtoYSSEBo-F0Zn_Rl9YjjTNgxcdXEQmOEk30BE6KCx67MdzQ2nuGqkQAdWO3SsK8kbSWJEFGrvcrfHPpFDZKA9qSZYsUw34WhBJ3eXP7vpy; aQQ_ajkguid=039DC33C-0142-4899-928D-3306CE716379; sessid=6A586865-53B3-4025-A44E-41229C751A59; ajk-appVersion=; ctid=27; fzq_h=0a9b12f276e9dcf85279f986229a0221_1704781929199_dd9674afcb7f4b2f9719927a8e40f73d_3740786969; obtain_by=2; twe=2; id58=CrIfoWWc6GlVO45LJSt2Ag==; ajk_member_verify=W5dL2pBhDCiir8akZBDo66rAU%2Bv0ns7yeYLQQhwQQ0U%3D; ajk_member_verify2=MjgwNjQwODk2fFVKYm5TcmJ8MQ%3D%3D; ajk_member_id=280640896; 58tj_uuid=8e523e03-5c23-4504-acff-0874ce7b85d8; init_refer=; new_uv=1; als=0; isp=true; new_session=0; wmda_uuid=c4c9f9d9224d8fee21bf554110d012a8; wmda_new_uuid=1; wmda_session_id_8788302075828=1704788119056-7b632064-addf-5ff8; wmda_visited_projects=%3B8788302075828; ved_loupans=523197; xxzlcid=e072e6594d5c42248ac7592958981727; xxzl-cid=e072e6594d5c42248ac7592958981727; lp_lt_ut=ab3c6d91895043f10b4c919ccc870946; xxzl_cid=d71dd6dc33c84bd19340f93a64c3470b; xxzl_deviceid=bnNnzFn1rNJNAyVjS3IS6Bc44muBh++I8YMgxvFR33vRRkETlizHoKTKLFAzcNgB; ajkAuthTicket=TT=7d4044ba9ee16217635773e28205dc41&TS=1704789514023&PBODY=XRU3EdSHMdJXJwGv0-tMnOU5wNMFLyqehFwf2PA7anPU-luRiVEG5Zueak6kIx5gwzisNUFpx78aYV8-a7p0Mo0jO4VGEXax7wg1UWnQg5LFcb6vReIYDcHpZp-H4qGQ6aqIz2Kqh66Xy5-Rl4tTPCn8Fu6lB9vhYyJt-4eBw8g&VER=2&CUID=z0xo8z93-p5iDJq5J3SnqaS2r3T3qS61',
            'if-modified-since': 'Tue, 09 Jan 2024 08:34:53 GMT',
            'referer': 'https://cs.fang.anjuke.com/loupan/all/p33/',
            'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'sec-fetch-dest': 'document',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-user': '?1',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
        }
    def get_html(self):

        resp = requests.get(url=self.url, headers=self.headers, cookies=self.cookies)
        resp.encoding = 'utf-8'
        return resp.text

    def parse_data(self,html):
        soup = BeautifulSoup(html, 'html.parser')
        links = soup.find_all("a", class_="lp-name")
        return links

    def save_data(self,links,Xurl_link):
        for link in links:
            href = link.get('href')
            href = href.replace('/loupan/', '/loupan/canshu-')
            Xurl_link.append(href)
            print(href)

    def run(self):

        html = self.get_html()
        links = self.parse_data(html)
        self.parse_data(html)
        Xurl_link = []
        self.save_data(links,Xurl_link)
        return Xurl_link

def main2():
    data = []
    url = "https://cs.fang.anjuke.com/loupan/all/"
    for i in range(1, 34):
        url_with_p = f"{url}p{i}"
        crawler = GetNewxiangqingList(url_with_p)
        Xurl_link = crawler.run()
        data.extend(Xurl_link)
    return data


if __name__ == '__main__':
    main2()